@extends('admin.layout.layout', ['row' => @$page])

@section('contain')

    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions">
            </div>
        </div>

        <div class="filter-wrapper">
            <form action="/{{ request()->path() }}/" class="form-inline filter-form">
                <div class=" input-group mb-3 custom-control-inline">
                    <input type="text" class="form-control s-input" name="value" value="{{ request()->input('value') }}" placeholder="{{ ts('CUSTOMERSEARCHTEXT') }}">
                </div>

                <x-region-chain-select></x-region-chain-select>

                @if(session('admin.user_type')!= 2)
                    {{--<x-distributor-select></x-distributor-select>--}}
                    <x-d-d-chain-select></x-d-d-chain-select>
                @else
                    <x-dealer-select></x-dealer-select>
                @endif

                <x-flatpickr-date-range label="{{ ts('ZBAZRQ') }}"></x-flatpickr-date-range>

                <div class="mb-3">
                    <button class="btn btn-primary">{{ ts('SEARCH') }}</button>
                    <a href="/{{ request()->path() }}/" class="btn btn-warning">{{ ts('RESET') }}</a>
                </div>
            </form>
        </div>
        <div>
            <div class="text-nowrap">
                <table class="table text-nowrap bt-table d-none">
                    <thead>
                    <tr class="order_column text-center">
                        <th class="text-center">{{ ts('SELECT') }}</th>
                        <th>{{ ts('sequence') }}</th>
                        <th data-field="customer_name" class=" {{ setOrderClass('customer_name') }}">{{ ts('customername') }}</th>
                        <th data-field="license_plate" class=" {{ setOrderClass('license_plate') }}">{{ ts('licenseplate') }}</th>
                        <th data-field="distributor_name" class=" {{ setOrderClass('distributor_name') }}">{{ ts('SUPERIORDISTRIBUTOR') }}</th>
                        <th data-field="dealer_name" class=" {{ setOrderClass('dealer_name') }}">{{ ts('SUPERIORDEALER') }}</th>
                        <th data-field="warranty_id" class=" {{ setOrderClass('warranty_id') }}">{{ ts('warrantyid') }}</th>
                        <th style="width:270px">{{ ts('OPERATIONS') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($data as $k => $v)
                        <tr class="item-list">
                            <td class="text-center">
                                <input type="checkbox" class="roll_icheck icheck_input" name="ids[]">
                            </td>
                            <td>{{ @$data->firstItem()+$k }}</td>
                            <td>{{ @$v->first_name }}</td>
                            <td>{{ @$v->license_plate }}</td>
                            {{--<td>{{ @$v->dealer_type == 2 ? '' :@$v->dealer_name }}</td>--}}
                            <td>
                                {{ getWDDName($v) }}
                                {{--{{ @$v->dealer_type == 2 ? @$v->dealer_name : @$v->distributor_name }}--}}
                            </td>
                            <td>
                                {{ getWDName($v) }}
                                {{--{{ @$v->dealer_name }}--}}
                            </td>
                            <td>{{ @$v->warranty_id }}</td>
                            <td  class="fixed-td text-center" style="width:270px">
                                @if (!empty($menus) && in_array('look', $menus))
                                    <a class="btn btn-success btn-sm" href='/{{ request()->path() }}/{{ $v->id }}'>{{ ts('VIEW') }}</a>
                                @endif
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>

            <div class="my-3">
                {{ $data->appends(request()->input())->links() }}
            </div>
        </div>


    </div>


@endsection

@section('script')
    <script>

        function getDealers() {
            var val = $('.distributor_json[name="distributor_id"]').val();
            var dealer_id = $('.dealer_json[name="dealer_id"]').data('value');
            if (val) {
                request.get('/admin/ajax/getUsers', {
                    params: {
                        t: 'a',
                        user_id: val
                    }
                })
                    .then( function(d) {
                        var data = d.data
                        $('.dealer_json[name="dealer_id"]').empty().append(makeSelectOptions(data, dealer_id))
                    })
                    .catch(function (d) {
                        toastr.error(d.msg)
                    })
            }
        }

        $(document).ready(function () {
            $(".distributor_json").select2();
            $(".dealer_json").select2();
            $('.table.bt-table').bootstrapTable({
                fixedColumns: true,
                fixedNumber: 1,
                fixedRightNumber: 1,
            }).removeClass('d-none');


            getDealers();
            $('.distributor_json[name="distributor_id"]').on('change', getDealers);
        })
    </script>
@endsection